package defiant.common.tools;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class DefiantBrowser implements MouseListener {
	
	private String appTitle = null;
	private String url = null;
	private String image = null;
	private boolean disposed = false;
	
	public boolean isDisposed() {
		return disposed;
	}
	
	public DefiantBrowser(String appTitle, String url, String icon) {
		this.appTitle = appTitle;
		this.url = url;
		this.image = icon;
	}
	
	  /**
	   * Runs the application
	   */
	  public void run() {
	    Display display = new Display();
	    Shell shell = new Shell(display);
	    shell.setText(appTitle);
	    
	    Image icon = new Image(display, image);
	    shell.setImage(icon);
	    
	    //createCoolBar(shell);
	    createContents(shell);
	    
	    shell.open();
	    shell.setFocus();
	    
	    //shell.forceFocus();
	    while (!shell.isDisposed()) {
	      if (!display.readAndDispatch()) {
	        display.sleep();
	      }
	    }
	    
	    disposed = shell.isDisposed();
	    shell.dispose();
	    display.dispose();
	  }

	  private void createCoolBar(Shell s) {
		  Display d = s.getDisplay();
		  
		    ToolBar toolbar = new ToolBar(s, SWT.NONE);
		    toolbar.setBounds(5, 5, 200, 30);

		    ToolItem goButton = new ToolItem(toolbar, SWT.PUSH);
		    goButton.setText("Go");

		    ToolItem backButton = new ToolItem(toolbar, SWT.PUSH);
		    backButton.setText("Back");

		    ToolItem stopButton = new ToolItem(toolbar, SWT.PUSH);
		    stopButton.setText("Stop");
		    
		    createContents(s);

		    //final Text text = new Text(s, SWT.BORDER);
		    //text.setBounds(5, 35, 400, 25);

		    //final Browser browser = new Browser(s, SWT.NONE);
		    //browser.setBounds(5, 75, 600, 400);
//		  final CoolBar bar = new CoolBar(s, SWT.BORDER);
//		    bar.setSize(280, 70);
//		    bar.setLocation(0, 0);
//		    
//		  final Image saveIcon = new Image(d, "images/24x24/anchor.png");
//		    final Image openIcon = new Image(d, "images/24x24/add.png");
//		    final Image cutIcon = new Image(d, "images/24x24/alarm.png");
//		    final Image copyIcon = new Image(d, "images/24x24/best.png");
//		    final Image pasteIcon = new Image(d, "images/24x24/bomb.png");
//		  
//		    final CoolItem saveCoolItem = new CoolItem(bar, SWT.PUSH);
//		    final Button saveBtn = new Button(bar, SWT.PUSH);
//		    saveBtn.setImage(saveIcon);
//		    saveBtn.pack();
//		    Point size = saveBtn.getSize();
//		    saveCoolItem.setControl(saveBtn);
//		    saveCoolItem.setSize(saveCoolItem.computeSize(size.x, size.y));
//		  
//		    final CoolItem openCoolItem = new CoolItem(bar, SWT.NONE);
//		    final Button openBtn = new Button(bar, SWT.PUSH);
//		    openBtn.setImage(openIcon);
//		    openBtn.pack();
//		    size = openBtn.getSize();
//		    openCoolItem.setControl(openBtn);
//		    openCoolItem.setSize(openCoolItem.computeSize(size.x, size.y));
	  }

//	  public void setFocus() {
//		  shell.forceFocus();
//	  }
	  
	  /**
	   * Creates the main window's contents
	   * 
	   * @param shell the main window
	   */
	  private void createContents(Shell shell) {
	    shell.setLayout(new FillLayout());

	    // Create a web browser
	    Browser browser = new Browser(shell, SWT.NONE);
	    browser.addMouseListener(this);
	    
	    // Navigate to page
	    browser.setUrl(url);
	  }

			  
	@Override
	public void mouseDoubleClick(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseDown(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseUp(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

//	  /**
//	   * The application entry point
//	   * 
//	   * @param args the command line arguments
//	   */
//	  public static void main(String[] args) {
//	    new DefiantBrowser().run();
//	  }
	}
